/**
 * @description:下载视频
 * @Author: wukai
 * @since: 2023-01-29 14:45:59
 * @return {*}
 */
import { message } from 'ant-design-vue';
const key = 'updatableing';
export default function downloadVideo(videoUrl: string, name: any) {
    message.success({ content: '正在下载中。。。', key, duration: 1 });
    const names: any = name.replaceAll(/\./gi, '点');
    videoUrl = videoUrl.replace(/\/\/|.+\/\//, '//');
    const a = document.createElement('a');
    fetch(videoUrl)
        .then((res) => {
            if (!res.ok) {
                throw new Error('网络响应失败');
            }
            return res.blob();
        })
        .then((blob: Blob) => {
            const objectUrl = window.URL.createObjectURL(blob);
            a.href = objectUrl;
            a.download = names;
            a.click();
            window.URL.revokeObjectURL(objectUrl);
        })
        .catch((err) => {
            console.log(err);
            message.error({ content: '下载失败，请通过预览链接在新窗口打开下载！', key, duration: 1 });
        })
        .finally(() => {
            a.remove();
            message.success({ content: '下载成功', key, duration: 1 });
        });
}
